package edu.java.texbooks.scjp.threads.test12;

/**
 * 
 * Test behaviour of Join() Method
 * 
 */
public class JoinMethodTest {

	private static final int TIME_INTERVAL = 1000;

	public static void main(String[] args) {
		// test join()
		// test1();

		// test join(int time_interval)
		test2();
	}

	public static void test1() {
		System.out.println("Start main thread.");

		MyThread at = new MyThread();
		Thread t = new Thread(at, "cecoThread");

		try {
			t.start();
			t.join();
		} catch (InterruptedException ie) {
			System.out.println("The current thread is interrupted.");
		}

		System.out.println(t.getName() + " is already dead and the "
				+ Thread.currentThread().getName()
				+ " thread continues its work.");
	}

	public static void test2() {
		System.out.println("Start main thread.");

		MyThread mt = new MyThread();
		Thread t = new Thread(mt, "svirchoThread");

		try {
			t.start();
			t.join(TIME_INTERVAL);
		} catch (InterruptedException ie) {
			System.out.println(t.getName() + " thread is interrupted.");
		}

		if (t.isAlive()) {
			System.out.println(t.getName() + " is alive"
					+ " but the time interval of " + TIME_INTERVAL / 1000
					+ " senond(s) is over and the "
					+ Thread.currentThread().getName()
					+ " thread continues its work.");
		} else {
			System.out.println(t.getName() + " thread is already dead and "
					+ "the " + Thread.currentThread().getName()
					+ " thread continues its work.");
		}

	}
}
